package com.thb.pay.payment.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.thb.pay.api.payment.PayTypeServiceApi;
import com.thb.pay.api.payment.vo.req.PayTypePageReq;
import com.thb.pay.api.payment.vo.req.PayTypeReq;
import com.thb.pay.api.payment.vo.resp.PayTypeVo;
import com.thb.pay.core.exception.RRException;
import com.thb.pay.core.utils.PageResult;
import com.thb.pay.db.entity.SuperEntity;
import com.thb.pay.db.utils.PageResultUtils;
import com.thb.pay.db.utils.PageUtils;
import com.thb.pay.db.utils.QueryUtils;
import com.thb.pay.payment.entity.PayType;
import com.thb.pay.payment.service.PayTypeService;
import com.xiaoleilu.hutool.bean.BeanUtil;
import com.xiaoleilu.hutool.collection.CollUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 支付-类型表 前端控制器
 * </p>
 *
 * @author lucky
 * @since 2019-07-21
 */
@Slf4j
@RestController
public class PayTypeController implements PayTypeServiceApi {

    @Autowired
    private PayTypeService payTypeService;

    @Override
    public PageResult<PayTypeVo> findPage(@RequestBody PayTypePageReq pageReq) {
        IPage<PayType> currentPage = PageUtils.buildPage(pageReq);
        QueryWrapper wrapper = QueryUtils.buildQueryPage(pageReq);
        IPage<PayType> pageResult = payTypeService.page(currentPage, wrapper);
        return PageResultUtils.build(pageResult, PayTypeVo.class);
    }

    @Override
    public List<PayTypeVo> findAllList() {
        List<PayTypeVo> resultDataList = Lists.newArrayList();
        List<PayType> targetDbResultList = payTypeService.selectList(null);
        targetDbResultList.forEach(dbResult -> {
            PayTypeVo resultData = new PayTypeVo();
            BeanUtil.copyProperties(dbResult, resultData);
            resultDataList.add(resultData);
        });
        return resultDataList;
    }

    @Override
    public List<PayTypeVo> findListByIds(@RequestBody List<Long> ids) {
        List<PayTypeVo> resultDataList = Lists.newArrayList();
        List<PayType> resDataList = payTypeService.selectBatchIds(ids);
        resDataList.forEach(payProduct -> {
            PayTypeVo resultData = new PayTypeVo();
            BeanUtil.copyProperties(payProduct, resultData);
            resultDataList.add(resultData);
        });
        return resultDataList;
    }

    @Override
    public PayTypeVo getById(@RequestParam("id") Long id) {
        PayTypeVo resultData = null;
        PayType dbResult = payTypeService.selectById(id);
        if (dbResult != null) {
            resultData = new PayTypeVo();
            BeanUtil.copyProperties(dbResult, resultData);
        }
        return resultData;
    }

    @Override
    public void add(@RequestBody PayTypeReq PayTypeReq) {
        LambdaQueryWrapper<PayType> wrapper = new LambdaQueryWrapper();
        wrapper.eq(PayType::getCode, PayTypeReq.getCode());
        List<PayType> targetDbResultList = payTypeService.selectList(wrapper);
        if (CollUtil.isNotEmpty(targetDbResultList)) {
            throw RRException.build("pay.type.exist");
        }
        PayType payType = new PayType();
        BeanUtil.copyProperties(PayTypeReq, payType);
        payTypeService.insert(payType);
    }

    @Override
    public void update(@RequestBody PayTypeReq PayTypeReq) {
        PayType payType = payTypeService.getById(PayTypeReq.getId());
        if (payType == null) {
            throw RRException.build("pay.type.not.exist");
        }
        if (!payType.getCode().equals(PayTypeReq.getCode())) {
            LambdaQueryWrapper<PayType> wrapper = new LambdaQueryWrapper();
            wrapper.eq(PayType::getCode, PayTypeReq.getCode());
            List<PayType> targetDbResultList = payTypeService.selectList(wrapper);
            if (CollUtil.isNotEmpty(targetDbResultList)) {
                throw RRException.build("pay.scene.exist");
            }
        }
        PayType targetPayType = new PayType();
        BeanUtil.copyProperties(PayTypeReq, targetPayType);
        payTypeService.updateById(targetPayType);
    }

    @Override
    public void deleteBatchIds(@RequestBody List<Long> ids) {
        LambdaQueryWrapper<PayType> wrapper = new LambdaQueryWrapper();
        wrapper.in(PayType::getId, ids);
        List<PayType> targetDbResultList = payTypeService.selectList(wrapper);
        if (CollUtil.isNotEmpty(targetDbResultList)) {
            targetDbResultList.forEach(payType -> {
                payType.setDelFlag(SuperEntity.DEFAULT_DELETE_FLAG);
            });
            payTypeService.updateBatchById(targetDbResultList);
        }
    }
}
